# test01_subprocess.py
# 调用 ping 命令
# ping -c 2 127.0.0.1 &> /dev/null  subprocess
# - 编写 ping 函数   def ping():
# - 用于测试远程主机的联通性
# - ping 通显示：x.x.x.x:up
# - ping 不通显示：x.x.x.x:down
import subprocess
def ping(ip):
    data = subprocess.run(
        f"ping -c 2 {ip} &> /dev/null", shell=True
    )
    if data.returncode == 0:
        print(f"{ip}: up")
    else:
        print(f"{ip}: down")
if __name__ == '__main__':
    ips = [f"192.168.1.{i}" for i in range(1, 255)]
    for host in ips:
        ping(host)
